package com.base.cn.platform.os.service.qa.thumb;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.dao.qa.thumb.QaThumbDao;
import com.base.cn.platform.os.entity.qa.question.QaQuestion;
import com.base.cn.platform.os.entity.qa.thumb.QaThumb;
import com.base.cn.platform.os.service.qa.question.QaQuestionBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问题点赞Biz
 *
 * @author LIZHIPENG
 * @create 2018-05-18 上午 9:18
 */
@Service
public class QaThumbBiz extends BaseBiz<QaThumb, QaThumbDao> {

    @Autowired
    private QaQuestionBiz qaQuestionBiz;

    /**
     * 问题点赞、取消点赞
     * @param questionId 问题ID
     * @param userId 用户ID
     * @return Map<String, Object>
     */
    @Transactional(rollbackFor = IllegalArgumentException.class)
    public Map<String, Object> thumbQuestion(BigDecimal questionId, BigDecimal userId){

        QaQuestion question = qaQuestionBiz.findQuestionById(questionId, false, false, false, false, false, false, false);
        if(ObjectUtils.isEmpty(question)){
            ResultUtil.ERROR("系统繁忙，请稍后重试");
        }

        int thumbNum = question.getThumbNum();
        Map<String, Object> thumbMap = new HashMap<>();
        List<QaThumb> qaThumbs = this.find("questionId=" + questionId + " and userId=" + userId, null, null);
        String message = "";
        if(ObjectUtils.isEmpty(qaThumbs)){
            QaThumb qaThumb = new QaThumb();
            qaThumb.setQuestionId(questionId);
            qaThumb.setUserId(userId);
            this.save(qaThumb);
            thumbNum += 1;
            thumbMap.put("thumbFlag", true);
            message = "点赞成功";
        }else {
            this.deleteById(qaThumbs.get(0).getId());
            thumbNum -= 1;
            thumbMap.put("thumbFlag", false);
            message = "取消点赞成功";
        }
        QaQuestion _question = new QaQuestion();
        _question.setId(questionId);
        _question.setThumbNum(thumbNum);
        qaQuestionBiz.addOrUpdateQuestion(_question);
        thumbMap.put("thumbNum", thumbNum);
        return ResultUtil.SUCCESS(message, thumbMap);
    }

    /**
     * 查询点赞列表
     * @param qaThumb 查询条件
     * @return  List<QaThumb>
     */
    public List<QaThumb> queryQaThumbList(QaThumb qaThumb){
        String whereSql = getWhereSql(qaThumb);
        return this.find(whereSql, qaThumb.getCount(), null);
    }

    /**
     * 拼接SQL语句
     * @param qaThumb 拼接条件
     * @return String
     */
    private String getWhereSql(QaThumb qaThumb){
        String whereSql = "1=1";
        if(ObjectUtils.isNotEmpty(qaThumb)){
            if(DataUtil.idIsNotNull(qaThumb.getUserId())){
                whereSql += " and userId="+ qaThumb.getUserId();
            }
            if(StringUtils.isNotEmpty(qaThumb.getQuestionIds())){
                whereSql += " and questionId in ("+ qaThumb.getQuestionIds() +")";
            }
        }
        whereSql += " order by id desc";
        return whereSql;
    }
}
